package com.exam.master.model.main.view;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.exam.commonbiz.base.BaseFragment;
import com.exam.commonbiz.base.BasicApplication;
import com.exam.commonbiz.bean.UserInfo;
import com.exam.commonbiz.util.CommonCallBack;
import com.exam.commonbiz.util.OnFastClickListener;
import com.exam.commonbiz.util.ToastUtil;
import com.exam.master.BuildConfig;
import com.exam.master.R;
import com.exam.master.WXApplication;
import com.exam.master.databinding.FragmentHomePersonalBinding;
import com.exam.master.model.login.LoginActivity;
import com.exam.commonbiz.model.IBindActivateCodeCallBack;
import com.exam.commonbiz.model.IBindChildPhoneCallBack;
import com.exam.commonbiz.model.IUserInfoCallBack;
import com.exam.commonbiz.viewmodel.CommonViewModel;
import com.exam.master.model.main.dialog.ActivateCodeDialog;
import com.exam.master.model.main.dialog.BindPhoneDialog;
import com.sanshao.commonui.dialog.CommonBottomDialog;
import com.sanshao.commonui.dialog.CommonDialogInfo;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class HomePersonalFragment extends BaseFragment<CommonViewModel, FragmentHomePersonalBinding> implements IBindActivateCodeCallBack, IUserInfoCallBack, IBindChildPhoneCallBack {

    public static HomePersonalFragment newInstance() {
        HomePersonalFragment fragment = new HomePersonalFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home_personal;
    }

    @Override
    public void initData() {

        mViewModel.setIBindActivateCodeCallBack(this);
        mViewModel.setIUserInfoCallBack(this);
        mViewModel.setIBindChildPhoneCallBack(this);
        binding.tvLogout.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                List<CommonDialogInfo> commonDialogInfoList = new ArrayList<>();
                commonDialogInfoList.add(new CommonDialogInfo("退出"));

                new CommonBottomDialog()
                        .init(context)
                        .setData(commonDialogInfoList)
                        .setOnItemClickListener(commonDialogInfo -> {
                            if (commonDialogInfo.position == 0) {
                                BasicApplication.logout();
                                LoginActivity.start(context);
                            }
                        })
                        .show();
            }
        });

        initUserInfo(WXApplication.getUserInfo());
    }

    public void getUserInfo() {
        if (mViewModel != null) {
            mViewModel.getUserInfo(WXApplication.getUserInfo().id);
        }
    }

    private void initUserInfo(UserInfo userInfo) {
        if (userInfo == null) {
            return;
        }
        binding.lcvPhone.setContent(userInfo.phone);

        binding.tvVersion.setText("V " + BuildConfig.VERSION_NAME);
        if (TextUtils.isEmpty(userInfo.childPhone)) {
            binding.lcvChildPhone.setContent("去绑定");
        } else {
            binding.lcvChildPhone.setContent(userInfo.childPhone);
        }

        // 账号被禁用，强制下线
        if (TextUtils.equals(userInfo.enable, "0")) {
            ToastUtil.showLongToastCenter("账号已被禁用");
            BasicApplication.logout();
            LoginActivity.start(context);
            getParentFragment().getActivity().finish();
        }
        if (!TextUtils.isEmpty(userInfo.expirationTime)) {
            // 已到期
            if (UserInfo.needActive(userInfo.expirationTime)) {
                binding.lcvExpirationTime.setContentColor(Color.RED);
                binding.lcvExpirationTime.setContent(userInfo.expirationTime + "(已到期)");
            } else {
                binding.lcvExpirationTime.setContentColor(R.color.color_333333);
                binding.lcvExpirationTime.setContent(userInfo.expirationTime);
            }
        }

        binding.lcvChildPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserInfo.needActive(userInfo.expirationTime)) {
                    ToastUtil.showLongToastCenter("请先激活账号");
                    return;
                }

                BindPhoneDialog bindPhoneDialog = new BindPhoneDialog();
                bindPhoneDialog.show(context, new CommonCallBack() {
                    @Override
                    public void callback(int postion, Object object) {
                        UserInfo userInfoBind = new UserInfo();
                        userInfoBind.phone = WXApplication.getUserInfo().phone;
                        userInfoBind.childPhone = (String) object;
                        mViewModel.bindChildPhone(userInfoBind);
                    }
                });
            }
        });

        binding.lcvActivate.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                ActivateCodeDialog activateCodeDialog = new ActivateCodeDialog();
                activateCodeDialog.show(context, new CommonCallBack() {
                    @Override
                    public void callback(int postion, Object object) {
                        UserInfo userInfoBind = new UserInfo();
                        userInfoBind.activateCode = (String) object;
                        mViewModel.bindActivateCode(userInfo.id, userInfoBind);
                    }
                });
            }
        });
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void returnBindActivateCode() {
        UserInfo userInfo = WXApplication.getUserInfo();
        mViewModel.getUserInfo(userInfo.id);
    }

    @Override
    public void returnUserInfo() {
        initUserInfo(WXApplication.getUserInfo());
    }

    @Override
    public void returnBindChildPhoneSuccess() {
        ToastUtil.showLongToastCenter("账号绑定成功");
        UserInfo userInfo = WXApplication.getUserInfo();
        mViewModel.getUserInfo(userInfo.id);
    }
}